﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Windows.Forms;
using ReaXML;
using SCH_Tools.Modules;

namespace SCH_Tools.DbManager
{

    class DBManagment
    {
        public Server MyServer { get; set; }

        private string GetConnectionString
        {
            get
            {
                XmlGenerate xml = new XmlGenerate();
                List<ServerDriver> obj = xml.GetListServerDriver(MyServer.ServerType);

                //SELECTION SERVER
                string server = string.Empty;
                if (!string.IsNullOrEmpty(MyServer.Alias))
                {
                    server = MyServer.Alias;
                }
                else
                {
                    if (!string.IsNullOrEmpty(MyServer.Host))
                    {
                        server = MyServer.Host;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(MyServer.Ip))
                        {
                            server = MyServer.Ip;
                        }
                    }
                }

                //SelectionMode PORT
                if (!string.IsNullOrEmpty(MyServer.Port))
                {
                    server = string.Format("{0},{1}", server, MyServer.Port);
                }

                String Conn = String.Format("Network Library=DBMSSOCN;Timeout=10;Data Source='{0}';User id={1};Password={2};", server, MyServer.User, MyServer.Pass);
                return Conn;
            }
        }

        public bool ConectDataBase()
        {
            try
            {
                OdbcConnection con = new OdbcConnection(GetConnectionString);
                con.Open();
                if (con.State == ConnectionState.Open)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return false;
        }

        public DataSet FillData(string sqlStatement)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(GetConnectionString);
                thisConnection.Open();

                if (thisConnection.State == ConnectionState.Open)
                {
                    SqlCommand thisCommand = thisConnection.CreateCommand();
                    thisCommand.CommandText = sqlStatement;

                    //SqlDataReader thisReader = thisCommand.ExecuteReader();

                    SqlDataAdapter thisAdapter = new SqlDataAdapter();
                    thisAdapter.SelectCommand = thisCommand;

                    DataSet resultSet = new DataSet();
                    thisAdapter.Fill(resultSet);

                    thisConnection.Close();

                    return resultSet;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }
    }
}
